<?php

namespace App\Models;

class Image extends Base
{
    //分页获取列表数据
    public function getPageList($options=[],$where=[],$whereDates){
        try{
            $limit = (isset($options['limit']) && $options['limit']) ? $options['limit'] : 1;
            $page = (isset($options['page']) && $options['page']) ? $options['page'] : 1;
            $model = $this->where($where);
            if(!empty($whereDates)){
                $model = $model->whereDate('created_at','>=',$whereDates[0])->whereDate('created_at','<',$whereDates[1]);
            }
            $model=$model->orderBy('created_at','desc');
            $result = $this->paging($model,$page,$limit);
            return $result;
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }
    }

    //编辑单张图片
    public function doUpdate($data){
        try {
            $now = date('Y-m-d H:i:s');
            $data['updated_at'] = $now;
            $res = $this->where('id',$data['id'])->update($data);
            if($res){
                return [
                    'code'=>0,
                    'data'=>$data['id']
                ];
            }else{
                return [
                    'code'=>1,
                    'message'=>'更新失败'
                ];
            }
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }
    }

    //添加单张轮播图
    public function oneInsert($data){
        try {
            $this->title = $data['title'];
            $this->title_zn = $data['title_zn'];
            $this->src = $data['src'];
            $this->type = $data['type'];
            $res = $this->save();
            if($res){
                return [
                    'code'=>0,
                    'data'=>$this->id
                ];
            }else{
                return [
                    'code'=>1,
                    'message'=>'添加失败'
                ];
            }
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }
    }
}
